/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.common.parsers.txt;

import com.blandware.atleap.common.Constants;
import com.blandware.atleap.common.parsers.SpecificPlainTextExtractor;
import com.blandware.atleap.common.parsers.exception.PlainTextExtractorException;

import java.io.*;


/**
 * An extractor that 'extracts' a plain text from a plain text. Created for
 * consistency.
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 * &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.3 $ $Date: 2005/08/14 12:27:55 $
 */
public class TXTPlainTextExtractor implements SpecificPlainTextExtractor {
    private static final int BUFFER_SIZE = 4096;
    private char[] buffer = new char[BUFFER_SIZE];

    protected String usedEncoding = null;

    public TXTPlainTextExtractor() {
    }

    /**
     * 'Extracts' a plain text from a plain text. Actually just converts bytes to
     * chars.
     *
     * @param input the input stream that supplies a plain text for extraction
     * @param output the writer that will accept the extracted text
     * @param encoding If specified, extractor assumes that the input text has
     * this <code>encoding</code>. If <code>null</code>, the
     * default encoding is used (currently <code>Constants.DEFAULT_ENCODING</code>).
     * @throws PlainTextExtractorException throwed on exception raised during
     * extracting
     */
    public void extract(InputStream input, Writer output, String encoding)
            throws PlainTextExtractorException {
        int readChars;

        try {
            if (encoding == null || encoding.trim().length() == 0) {
                encoding = Constants.DEFAULT_ENCODING;
            }
            usedEncoding = encoding;
            Reader reader = new BufferedReader(new InputStreamReader(input, encoding));
            while (true) {
                readChars = reader.read(buffer);
                if (readChars <= 0) {
                    break;
                }
                output.write(buffer, 0, readChars);
            }
        } catch (IOException e) {
            throw new PlainTextExtractorException(e);
        }
    }

    /**
     * @see com.blandware.atleap.common.parsers.SpecificPlainTextExtractor#getUsedEncoding()
     */
    public String getUsedEncoding() {
        return usedEncoding;
    }
}
